﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Waiter.Common;
using Waiter.Controler;
using DevComponents.DotNetBar.Controls;
using DevComponents.DotNetBar;

namespace Waiter.View
{
    public partial class frmHoursWork_RefList : DevComponents.DotNetBar.Office2007Form//Form
    {
        bool bIsUpdateNote = true;
        BindingSource myBinding = new BindingSource();
        HoursWorkCtrl myHourWorkControl = new HoursWorkCtrl();
        StaffCtrl myStaffControl = new StaffCtrl();
        public delegate void CloseForm();                       //Delegate close form
        public CloseForm myCloseForm;                           //Tạo thể hiện của delegate

        public frmHoursWork_RefList()
        {
            InitializeComponent();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "frmHoursWork_RefList.btnSearch_Click", "", "");

            //Kiểm tra thứ tự của ngày from - to
            if (dateTimeKeepingFrom.Value > dateTimeKeepingTo.Value)
            {
                MessageBoxEx.Show("Xin hãy kiểm tra lại khoảng thời gian chấm công cần tra cứu.", CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                dateTimeKeepingTo.Focus();
                myError.SetError(dateTimeKeepingTo, "Ngày bắt đầu đang lớn hơn ngày kết thúc");
                return;
            }

            myError.Clear();
            bIsUpdateNote = false;
            myBinding.DataSource = myHourWorkControl.fsLoadRefGridData(dateTimeKeepingFrom.Value, new DateTime(dateTimeKeepingTo.Value.Year, dateTimeKeepingTo.Value.Month, dateTimeKeepingTo.Value.Day, 23, 59, 59), cmbStaff.SelectedValue.ToString());
            grdWorkedList.DataSource = myBinding;
            bIsUpdateNote = true;
            grdWorkedList_SelectionChanged(new object(), new EventArgs());
        }

        /// <summary>
        /// Form load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmHoursWork_RefList_Load(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "frmHoursWork_RefList.frmHoursWork_RefList_Load", "", "");

            //Load data cho các control
            myStaffControl.fsLoadComboBox_ListStaff(cmbStaff);

            //Load trị mặc định cho các control            
            dateTimeKeepingFrom.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            dateTimeKeepingTo.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month));
        }

        private void headerItem1_Load(object sender, EventArgs e)
        {
            this.headerItem1.init("TRA CỨU THÔNG TIN CHẤM CÔNG", false);
            //this.headerItem1.btnChangeStyle.Visible = false;
            //this.headerItem1.btnSwitchUser.Visible = false;
            //this.headerItem1.lblShift.Visible = false;
            //this.headerItem1.lblShift1.Visible = false;
            ////this.headerItem1.lblName.Text = Waiter.Properties.Settings.Default.STAFF_LOGIN_NAME;
        }

        private void grdWorkedList_SelectionChanged(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "frmHoursWork_RefList.grdWorkedList_SelectionChanged", "", "");

            if ((this.grdWorkedList).CurrentCell == null)//grid không có data
            {
                txtNote1.Text = string.Empty;
                txtNote2.Text = string.Empty;
                txtNote3.Text = string.Empty;
                txtNote4.Text = string.Empty;
                return;
            }

            if (grdWorkedList.CurrentCell.RowIndex > -1 && bIsUpdateNote == true)
            {
                int iCurrentRowIndex = grdWorkedList.CurrentCell.RowIndex;
                if (grdWorkedList.Rows[iCurrentRowIndex].Cells["colNote1"].Value != null)
                {
                    txtNote1.Text = grdWorkedList.Rows[iCurrentRowIndex].Cells["colNote1"].Value.ToString();
                }
                else
                {
                    txtNote1.Text = string.Empty;
                }
                if (grdWorkedList.Rows[iCurrentRowIndex].Cells["colNote2"].Value != null)
                {
                    txtNote2.Text = grdWorkedList.Rows[iCurrentRowIndex].Cells["colNote2"].Value.ToString();
                }
                else
                {
                    txtNote2.Text = string.Empty;
                }
                if (grdWorkedList.Rows[iCurrentRowIndex].Cells["colNote3"].Value != null)
                {
                    txtNote3.Text = grdWorkedList.Rows[iCurrentRowIndex].Cells["colNote3"].Value.ToString();
                }
                else
                {
                    txtNote3.Text = string.Empty;
                }
                if (grdWorkedList.Rows[iCurrentRowIndex].Cells["colNote4"].Value != null)
                {
                    txtNote4.Text = grdWorkedList.Rows[iCurrentRowIndex].Cells["colNote4"].Value.ToString();
                }
                else
                {
                    txtNote4.Text = string.Empty;
                }
            }
        }

        /// <summary>
        /// Khi đóng đang đóng
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmStaff_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (myCloseForm != null)//Thông báo cho form menu
                myCloseForm();
        }
    }
}
